﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ModubsRTUTempactureCurve.Conponents
{
    /// <summary>
    /// Meter.xaml 的交互逻辑
    /// </summary>
    public partial class Meter : System.Windows.Controls.UserControl
    {
        public Meter()
        {
            InitializeComponent();
            this.SizeChanged += Meter_SizeChanged;
        }

        private void Meter_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.RefreshScale();
        }

        double radius = 50;
        double step = 0.0;
        double minValue = -20, maxValue = 100;
        private void RefreshScale()
        {
            this.root.Width = Math.Min(RenderSize.Width, RenderSize.Height);
            radius = root.Width / 2;
            if (double.IsNaN(radius) || radius < 0)
            { return; }
            scale_canvas.Children.Clear();
            int interval = 10;
            step = 270.0 / (maxValue - minValue );
            for (int i = 0;i <= maxValue - minValue; i++)
            {
                if (i % 10 != 0) continue;
                double angle = step * i - 45;
                Line lineScale = new Line();
                lineScale.Stroke = System.Windows.Media.Brushes.White;
                lineScale.StrokeThickness = 1;
                //lineScale.Opacity = 0.3;
                lineScale.X1 = radius - (radius - 15) * Math.Cos(angle * Math.PI / 180);
                lineScale.Y1 = radius - (radius - 15) * Math.Sin(angle * Math.PI / 180);
                lineScale.X2 = radius - (radius - 12) * Math.Cos(angle * Math.PI / 180);
                lineScale.Y2 = radius - (radius - 12) * Math.Sin(angle * Math.PI / 180);
                this.scale_canvas.Children.Add(lineScale);
                TextBlock txtScale = new TextBlock();
                txtScale.Text = (minValue + interval * i / 10).ToString();
                txtScale.Width = 34;
                txtScale.FontSize = 9;
                txtScale.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FFF")); 
                //txtScale.Opacity = 0.3;
                txtScale.TextAlignment = TextAlignment.Center;

                Canvas.SetLeft(txtScale, radius - (radius - 2) * Math.Cos(angle * Math.PI / 180) -19);
                Canvas.SetTop(txtScale, radius - (radius - 2) * Math.Sin(angle * Math.PI / 180) -5);
                this.scale_canvas.Children.Add(txtScale);

            }
            string path_data_str = $"M{radius * 0.3} {radius}" +
                $"A{radius*0.7} {radius * 0.7} 0 1 1 {radius} {radius *1.7}";
            this.path.Data=Geometry.Parse(path_data_str);

            this.RefreshValue();
        }

        private void RefreshValue()
        {
            double newAngle = ((Value - -20) / (maxValue - minValue) * 270);
            double valueX = radius - (radius * 0.7) * Math.Cos(newAngle * Math.PI / 180);
            double valueY = radius - (radius * 0.7) * Math.Sin(newAngle * Math.PI / 180);
            string pathvalue_str = $"M{radius * 0.3} {radius}" +
                $"A{radius * 0.7} {radius * 0.7} 0 {(newAngle > 180 ? 1 : 0)} 1 {valueX} {valueY}";
            this.path_value.Data = Geometry.Parse(pathvalue_str);
        }
        
        public double Value
        {
            get { return (double)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(double), typeof(Meter), new PropertyMetadata(0.0,ValueChangedCallback));

        private static void ValueChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

            if(d is Meter meter)
            {
                meter.RefreshValue();
            }

        }
    }
}
